package gui;

import library.*;
import javax.swing.*;
import java.awt.event.*;

public class LibraryMainFrame extends JFrame {
    private Library library;
    private JComboBox<String> resourceComboBox;
    private JTextArea displayArea;
    private JTextField borrowerNameField;

    public LibraryMainFrame() {
        library = new Library();

        setTitle("图书馆管理系统");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);

        JLabel label = new JLabel("资源列表:");
        label.setBounds(20, 20, 100, 25);
        add(label);

        resourceComboBox = new JComboBox<>();
        updateResourceList();
        resourceComboBox.setBounds(120, 20, 200, 25);
        add(resourceComboBox);

        JButton borrowButton = new JButton("借阅");
        borrowButton.setBounds(340, 20, 80, 25);
        borrowButton.addActionListener(this::borrowAction);
        add(borrowButton);

        JButton returnButton = new JButton("归还");
        returnButton.setBounds(430, 20, 80, 25);
        returnButton.addActionListener(this::returnAction);
        add(returnButton);

        JLabel borrowerLabel = new JLabel("借阅人姓名:");
        borrowerLabel.setBounds(20, 60, 100, 25);
        add(borrowerLabel);

        borrowerNameField = new JTextField();
        borrowerNameField.setBounds(120, 60, 200, 25);
        add(borrowerNameField);

        displayArea = new JTextArea();
        displayArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(displayArea);
        scrollPane.setBounds(20, 100, 550, 250);
        add(scrollPane);

        JButton viewButton = new JButton("查看借阅记录");
        viewButton.setBounds(340, 60, 150, 25);
        viewButton.addActionListener(this::viewBorrowRecord);
        add(viewButton);

        setVisible(true);
    }

    private void updateResourceList() {
        resourceComboBox.removeAllItems();
        for (Resource r : library.getAllResources()) {
            resourceComboBox.addItem(r.getId() + " - " + r.getTitle());
        }
    }

    private void borrowAction(ActionEvent e) {
        String selected = (String) resourceComboBox.getSelectedItem();
        if (selected == null) return;
        String id = selected.split(" ")[0];

        String name = borrowerNameField.getText();
        if (name.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入借阅人姓名");
            return;
        }
        Resource resource = library.findResourceById(id);
        Borrower borrower = library.getBorrower(name);
        if (borrower == null) {
            borrower = new Borrower(name);
            library.addBorrower(borrower);
        }
        if (borrower.borrow(resource)) {
            JOptionPane.showMessageDialog(this, "借阅成功！");
            updateResourceList();
            library.saveResourcesToFile();
        } else {
            JOptionPane.showMessageDialog(this, "该资源不可用！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void returnAction(ActionEvent e) {
        String selected = (String) resourceComboBox.getSelectedItem();
        if (selected == null) return;
        String id = selected.split(" ")[0];

        String name = borrowerNameField.getText();
        if (name.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入借阅人姓名");
            return;
        }
        Resource resource = library.findResourceById(id);
        Borrower borrower = library.getBorrower(name);
        if (borrower != null && borrower.returnResource(resource)) {
            JOptionPane.showMessageDialog(this, "归还成功！");
            updateResourceList();
            library.saveResourcesToFile();
        } else {
            JOptionPane.showMessageDialog(this, "未找到借阅记录！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void viewBorrowRecord(ActionEvent e) {
        String name = borrowerNameField.getText();
        if (name.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入借阅人姓名");
            return;
        }
        Borrower borrower = library.getBorrower(name);
        if (borrower == null) {
            displayArea.setText("无借阅记录");
            return;
        }
        StringBuilder sb = new StringBuilder();
        for (Resource r : borrower.getBorrowedResources()) {
            sb.append(r).append("\n");
        }
        displayArea.setText(sb.toString());
    }
}